package com.yyfax.infrastructure.upgrade;

import android.app.Activity;
import android.content.Context;
import com.yyfax.common.CLog;

/**
 * Created by HenryZhang on 2016/11/23.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class Updater {
    private static final String TAG = "Updater";

	public static final int RESULT_CODE_KILL = 1010;

    private static boolean mChecked = false;
	private static boolean mDialogHadShowed = false;

	private static CheckVersionInfoTask.VersionInfo mVersionInfo = new CheckVersionInfoTask.VersionInfo();

    private static Handler mHandler;

    public abstract static class Handler {
		protected String url;
		protected String params;

		public Handler(String target, String p) {
			url = target;
			params = p;
		}

        public abstract void onVersionInfoDownloaded(CheckVersionInfoTask.VersionInfo info);
    }

	private static void showUpdateDialog(Activity aty, CheckVersionInfoTask.VersionInfo info) {
		CLog.i(TAG, "info=" + info.msg);
		if (!info.hasNewVersion) return;
		UpdateDialog.show(aty, info);
		setDialogHadShowed(true);
	}

	public static void checkVersion(final Activity aty, String url, String params, final String applicationId) {
		if (mChecked && needShowUpdateDialog()) {
			mVersionInfo.applicationId = applicationId;
			showUpdateDialog(aty, mVersionInfo);
		} else {
			checkVersion(aty, CheckVersionInfoTask.MODE_CUSTOM, new Updater.Handler(url, params) {
				@Override
				public void onVersionInfoDownloaded(CheckVersionInfoTask.VersionInfo info) {
					info.applicationId = applicationId;
					showUpdateDialog(aty, info);
				}
			});
		}
	}

    public static void checkVersion(Context ctx, int mode, Handler handler) {
        CLog.d(TAG, "checkVersion");
        if (mChecked) {
            handler.onVersionInfoDownloaded(new CheckVersionInfoTask.VersionInfo());
            return;
        }

	    mHandler = handler;

        Assistant.executeAsyncTaskParallel(new CheckVersionInfoTask(ctx, handler.url, handler.params, mode) {
            @Override
            protected void onPostExecute(Object obj) {
                VersionInfo info;
                if (obj == null) info = new VersionInfo();
                else info = (VersionInfo) obj;

                CLog.d(TAG, "hasNewVersion:" + info.hasNewVersion
		                + " forceUpdate:" + info.forceUpdate
						+ " versionCode:" + info.versionCode);

                mVersionInfo = info;

                final Handler handler = mHandler;
                if (handler != null) {
                    handler.onVersionInfoDownloaded(info);
                    mChecked = true;
                }
            }
        });
    }

	public static CheckVersionInfoTask.VersionInfo getUpdateInfo() {
		return mVersionInfo;
	}

	public static void setDialogHadShowed(boolean showed) {
		mDialogHadShowed = showed;
	}

    public static boolean needShowUpdateDialog() {
        return (mVersionInfo.hasNewVersion && mVersionInfo.forceUpdate) || (mVersionInfo.hasNewVersion && !mDialogHadShowed);
    }
}
